package com.uptown.matrix.core.producer.instance;

import java.util.Properties;

import com.alibaba.fastjson.JSON;
import com.uptown.matrix.common.result.WebResult;
import com.uptown.matrix.core.producer.ProducerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import sun.rmi.runtime.RuntimeUtil;

@Slf4j
@Service
public class RocketMqProducerService extends ProducerService {


    @Override
    public WebResult producerSend(String bootServer, String group, String topic, String message) {
        super.producerSend(bootServer, group, topic, message);

        DefaultMQProducer producer = new DefaultMQProducer(group);
        producer.setNamesrvAddr(bootServer);
        producer.setInstanceName(System.currentTimeMillis() + topic);
        producer.setMaxMessageSize(10240);
        producer.setSendMsgTimeout(3000);
        producer.setVipChannelEnabled(false);

        Message messageToUser = new Message(topic, "*", message.getBytes());
        try {
            producer.start();
            return WebResult.of(producer.send(messageToUser));
        } catch (Exception e) {
            log.error("消息发送异常 ", e);
            return WebResult.fail(e.getMessage());
        } finally {
            producer.shutdown();
        }
    }

    @Override
    public WebResult topicList() {
        return null;
    }
}
